#include "showpatient.h"
#include "ui_showpatient.h"
#include <QDate>
showPatient::showPatient(QString ID,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::showPatient)
{
    ui->setupUi(this);
    id=ID;
    ui->caseTable->setSelectionBehavior ( QAbstractItemView::SelectRows);
    ui->caseTable->setSelectionMode(QAbstractItemView::SingleSelection);
    connect(ui->showBtn,&QPushButton::clicked,this,&showPatient::showcase);
    connect(ui->newBtn,&QPushButton::clicked,this,&showPatient::newcase);
    connect(ui->refreshBtn,&QPushButton::clicked,this,&showPatient::refresh);
    connect(ui->caseTable,&QTableWidget::cellClicked,this,&showPatient::recnum);
    connect(ui->delBtn,&QPushButton::clicked,this,&showPatient::del);
}

void showPatient::del()
{
    QPushButton *okbtn = new QPushButton("确定");
    QPushButton *nobtn = new QPushButton("取消");
    QMessageBox *mymsgbox = new QMessageBox;
    mymsgbox->setIcon(QMessageBox::Warning);
    mymsgbox->setWindowTitle("提示");
    mymsgbox->setText("确定要删除此条病例吗?");
    mymsgbox->addButton(okbtn, QMessageBox::AcceptRole);
    mymsgbox->addButton(nobtn, QMessageBox::RejectRole);
    mymsgbox->show();
    mymsgbox->exec();//等待用户响应
    if (mymsgbox->clickedButton()==okbtn)//点击了OK按钮
    {
        qDebug()<<"deleting";
        l->delcase(list[selected_case].num);
        refresh();
        l->exportToExcel();
        cwin->saved();
        mymsgbox->hide();
    }
    if(mymsgbox->clickedButton()==nobtn)
    {
        mymsgbox->hide();
    }
    return;
}

void showPatient::setcwin(showCase *w)
{
    cwin=w;
    connect(cwin,&showCase::saved,this,&showPatient::refresh);
}

void showPatient::refresh()
{
    illcase lis[1000];
    int nn=l->askillcasesByID(id,lis);
    this->setlist(lis,nn);
    for(int i=0;i<nn;i++)
    {
        list[i]=lis[i];
        qDebug()<<list[i].num;
    }
}

void showPatient::newcase()
{
    qDebug()<<"init case";
    QDate date = QDate::currentDate();
    QString pp[30],mm[30];
    pp[0]=date.toString("yyyy.MM.dd");
    for(int i=1;i<=7;i++)
    {
        pp[i]=list[0][l->items[i]];
    }
    qDebug()<<"new case";
    l->addcase(pp,mm,0);
    qDebug()<<"cwin init"<<l->a.length()-1<<(*l)[l->a.length()-1].num;
    cwin->init(l,(*l)[l->a.length()-1].num);
    cwin->show();
}

void showPatient::recnum(int x, int y)
{
    selected_case=x;
}

void showPatient::showcase()
{
    qDebug()<<"selected_case:"<<selected_case;
    qDebug()<<"casenum:"<<list[selected_case].num;
    cwin->init(l,list[selected_case].num);
    cwin->show();
}

void showPatient::setillcases(illcases *ll)
{
    l=ll;
    this->refresh();
}

void showPatient::setlist(illcase *ll, int num)
{
    ui->infoLabel
            ->setText(  "日期:"+ll[0]["date"]+
                         "\t\t医院:"+ll[0]["hospital"]+
                         "\t\t姓名:"+ll[0]["name"]+
                         "\t\t性别:"+ll[0]["sex"]+
                         "\n医保类型:"+ll[0]["health_insurance_type"]+
                         "\t\t门诊号:"+ll[0]["ID"]+
                         "\t\t年龄:"+ll[0]["age"]);
    ui->caseTable->setRowCount(num);
    for(int i=0;i<num;i++)
    {
        list[i]=ll[i];
        QTableWidgetItem *p[10];
        for(int j=0;j<7;j++)
            p[j]=new QTableWidgetItem;

        for(int j=8;j<=14;j++)
        {
            p[j-8]->setText(list[i][l->items[j]]);
        }

        for(int j=0;j<7;j++)
            ui->caseTable->setItem(i,j,p[j]);
    }
    ui->caseTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);    //x先自适应宽度
    for(int i=0;i<7;i++)
    ui->caseTable->horizontalHeader()->setSectionResizeMode(i, QHeaderView::ResizeToContents);     //然后设置要根据内容使用宽度的列
}

showPatient::showPatient(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::showPatient)
{
    ui->setupUi(this);
}
showPatient::~showPatient()
{
    delete ui;
}
